<blockquote class="layui-elem-quote">商品列表管理</blockquote>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" id="keyword" name="goodsname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
        </script>


    </div>
</div>
<!--<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>-->
<script >
    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            upload = layui.upload,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/api/goods/page',
            toolbar: '#toolbarDemo',
            limits: [5, 10, 15, 20, 100],
            limit: 10,
            page: true,
            skin: 'line',
            request:{
              pageName: 'pageNum',
              limitName: 'pageSize'
            },
            response:{
              statusCode:10000
            },
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            },
            cols: [[
                {field: 'id',  title: 'ID', sort: true},
                {field: 'name', title: '名称'},
                {field: 'dscp', title: '描述'},
                {field: 'price', title: '价格', sort: true},
                {field: 'markPrice', title: '标价'},
                {field: 'color', title: '颜色'},
                {field: 'version', title: '版本'},
                {field: 'count', title: '数量'},
                {field: 'content', title: '内容'},
                {field: 'pic', title: '图片', templet:function (d) {
                    let picArr = d.picList;
                    let str = "";
                    for (let i=0 ; i<picArr.length; i++){
                        str += `<img src="/upload/${picArr[i].url}" style="width: 30px; height: 30px;" />`
                    }

                        return str;
                    }},
                {field: 'categoryId', title: '所属类别', templet:function (d) {
                        if(d.category){
                            return d.category.name;
                        }else {
                            return "";
                        }

                    } },
                {field: 'recom', title: '是否推荐', templet:function (d) {
                        return ['不推荐','推荐'][d.recom]
                    }},
                {field: 'status', title: '是否上架', templet:function (d) {
                        return ['上架','不上架'][d.status]
                    }},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
        });

        //监听搜索按钮时间
        $("#keyword").on("input",function (){
            let name = $(this).val();
            table.reload("currentTableId", {
                page: {
                    curr:1
                },
                where:{
                    name: name,
                    dscp: name
                }
            })
        })


        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add')
            layer.open({
                type:2,
                title:"添加分类信息",
                area:['800px','600px'],
                content:'/goods/goodsAdd.html',
                end: function () {
                    table.reload("currentTableId")
                }
            })
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                sessionStorage.setItem("id",data.id);
                layer.open({
                    type:2,
                    title:"修改分类信息",
                    area:['800px','600px'],
                    content:'/goods/goodsUpdate.html',
                    end: function () {
                        table.reload("currentTableId")
                    }
                })


            } else if (obj.event === 'delete') {
                layer.confirm(`您确定要删除【${data.name}】分类吗？`,{title:'删除确认'}, function (index) {
                    $.ajax({
                        url:`/api/goods/${data.id}`,
                        type:"DELETE",
                        dataType:'JSON',
                        success:function (resp) {
                            layer.msg(resp.msg);
                            if(resp.code === 10000){
                                table.reload("currentTableId",{
                                    page: {
                                        curr:1
                                    }
                                })
                                layer.close(index);
                            }
                        }
                    })
                });
            }
        });

    });
</script>
